package com.cyh.nio;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

/**
 *  charset使用
 * */
public class CharsetDemo01 {


    public static void main(String[] args) throws Exception {
        //1 获取Charset对象
        Charset charset = Charset.forName("UTF-8");

        //2 获取编码器对象
        CharsetEncoder charsetEncoder = charset.newEncoder();

        //3 创建缓冲区
        CharBuffer charBuffer = CharBuffer.allocate(1024);
        charBuffer.put("hello 陈益洪");
        charBuffer.flip();

        //4 编码
        ByteBuffer byteBuffer = charsetEncoder.encode(charBuffer);
        System.out.println("编码后的结果：");
        for(int i = 0; i < byteBuffer.limit(); i++) {
            System.out.println(byteBuffer.get());
        }


        //5 获取解码器对象
        byteBuffer.flip();
        CharsetDecoder charsetDecoder = charset.newDecoder();

        //6 解码
        CharBuffer charBuffer1 = charsetDecoder.decode(byteBuffer);
        System.out.println("解码后的结果：");
        System.out.println(charBuffer1.toString());

    }
}
